{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入所需库\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pymatgen import MPRester\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 材料项目(Materials Project)数据库为每个用户生成的API_KEY\n",
    "MY_API_KEY = 'YOUR_API_KEY'\n",
    "\n",
    "# 通过API_KEY获取许可\n",
    "m = MPRester(MY_API_KEY)\n",
    "\n",
    "# 获取材料数据\n",
    "query_data = m.query(\n",
    "    \n",
    "    # 设置获取的材料种类\n",
    "    criteria = {\n",
    "        \"e_above_hull\": {\"$lt\": 0.1},  # 凸包能量大于等于0.1eV,表明获取的是处于稳态或者亚稳态的材料\n",
    "        \"nelements\" : { \"$gte\" :  2 },  # 材料的元素组成大于等于2，表明获取的是化合物\n",
    "        \"magnetic_type\":{\"$in\":[\"FM\", \"FiM\", \"AFM\", \"NM\"]},  # 表明获取的是铁磁，反铁磁，亚铁磁，顺磁材料\n",
    "        'warnings' : [],  # 没有警告\n",
    "    }, \n",
    "    \n",
    "    # 设置获取的材料性能\n",
    "    properties = [\n",
    "        'material_id', 'pretty_formula', 'unit_cell_formula', 'nsites', 'spacegroup.number', 'cif', \n",
    "        'magnetic_type', 'magnetism.total_magnetization'\n",
    "    ]\n",
    ")\n",
    "\n",
    "# 显示材料的获取进度\n",
    "query_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将材料数据存为json文件\n",
    "filename = \"Inorganic_magnetic_materials_data_20210606.json\"\n",
    "with open(filename, 'w') as file_obj:\n",
    "    json.dump(query_data, file_obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取json文件\n",
    "filename = \"Inorganic_magnetic_materials_data_20210606.json\"\n",
    "with open(filename) as file_obj:\n",
    "    numbers = json.load(file_obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通过读取的json文件将数据存为csv格式\n",
    "query_data_form = pd.DataFrame(\n",
    "    columns = [\n",
    "        'material_id', 'pretty_formula', 'unit_cell_formula', 'nsites', 'spacegroup.number', 'cif', \n",
    "        'magnetic_type', 'magnetism.total_magnetization'\n",
    "    ], \n",
    "    data = numbers,\n",
    ")\n",
    "# 存储csv格式的材料数据格式\n",
    "query_data_form.to_csv('Inorganic_magnetic_materials_data_20210606.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 显示获取到的cvs格式的材料数据\n",
    "query_data_form"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 展示获取到无机磁性材料数据的大致信息\n",
    "ferromagnets = query_data_form.loc[query_data_form[\"magnetic_type\"] == \"FM\"]\n",
    "ferrimagnets = query_data_form.loc[query_data_form[\"magnetic_type\"] == \"FiM\"]\n",
    "paramagnets = query_data_form.loc[query_data_form[\"magnetic_type\"] == \"NM\"]\n",
    "antiferromagnets = query_data_form.loc[query_data_form[\"magnetic_type\"] == \"AFM\"]\n",
    "\n",
    "print(f\"{len(ferromagnets)} ferromagnetism materials\")\n",
    "print(f\"{len(ferrimagnets)} ferrimagnetism materials\")\n",
    "print(f\"{len(paramagnets)} paramagnetism materials\")\n",
    "print(f\"{len(antiferromagnets)} antiferromagnetism materials\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
